﻿using BlueFox.MsgPush.Core;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace BlueFox.MsgPush
{
    public static class UsePushExtend
    {
        public static IServiceCollection UsePush(this IServiceCollection services,
            IConfigurationSection configurationSection)
        {
            var config = configurationSection.Get<Config>();
            PushService.Create(config.Appkey, config.AppMasterSecret);
            return services;
        }
    }

    public static class PushService
    {
        private static UMengMessagePush uMeng;

        public static void Create(string appkey,string appMasterSecret)
        {
            if (uMeng == null)
            {
                uMeng = new UMengMessagePush(appkey, appMasterSecret);
            }
        }

        public static async Task<ReturnJsonClass> Push(PostUMengJson postJson)
        {
            ReturnJsonClass result=null;
            uMeng.AsynSendMessage(postJson,res=> {
                result = res;
            });
            var time = DateTime.Now.AddMinutes(1);
            return await Task.Run(() =>
            {
                while (result == null)
                {
                    if (time < DateTime.Now)
                    {
                        result = new ReturnJsonClass();
                        result.ret = "FAIL";
                        result.data = new ResultInfo() { error_code = "超时没返回!" };
                        break;
                    }
                }
                return result;
            });
        }

        public static async Task<ReturnJsonClass> PushNotification(string deviceTokens,string ticker,string title,string body)
        {
            PostUMengJson postJson = new PostUMengJson();
            postJson.type = "unicast";
            postJson.device_tokens = deviceTokens;
            postJson.payload = new Payload();
            postJson.payload.display_type = "notification";
            postJson.payload.body = new ContentBody();
            postJson.payload.body.ticker = ticker;
            postJson.payload.body.title = title;
            postJson.payload.body.text = body;
            postJson.payload.body.after_open ="app";
            postJson.thirdparty_id = "COMMENT";
            return await Push(postJson);
        }

        public static async Task<ReturnJsonClass> PushMessage(string deviceTokens, string ticker, string title, string body)
        {
            PostUMengJson postJson = new PostUMengJson();
            postJson.type = "unicast";
            postJson.device_tokens = deviceTokens;
            postJson.payload = new Payload();
            postJson.payload.display_type = "message";
            postJson.payload.body = new ContentBody();
            postJson.payload.body.ticker = ticker;
            postJson.payload.body.title = title;
            postJson.payload.body.text = body;
            postJson.payload.body.after_open = "app";
            postJson.thirdparty_id = "COMMENT";
            return await Push(postJson);
        }

        public static async Task<ReturnJsonClass> PushBroadcast(string deviceTokens, string ticker, string title, string body)
        {
            PostUMengJson postJson = new PostUMengJson();
            postJson.type = "broadcast";
            postJson.device_tokens = deviceTokens;
            postJson.payload = new Payload();
            postJson.payload.display_type = "message";
            postJson.payload.body = new ContentBody();
            postJson.payload.body.ticker = ticker;
            postJson.payload.body.title = title;
            postJson.payload.body.text = body;
            postJson.payload.body.after_open = "app";
            postJson.thirdparty_id = "COMMENT";
            return await Push(postJson);
        }
    }
}
